#include <QQmlApplicationEngine>
#include <QGuiApplication>
#include <QSharedMemory>
#include <warning.h>

#include <src/Thread/threadbase.h>
#include <src/Entity/clientdefect.h>
#include "src/Models/steelserverresponse.h"
#include "src/Models/NormalDefectModels.h"
#include "src/Utils/inifilehelper.h"
#include"include/logger.h"
#include <QMessageBox>
#include "src/Strategypatttern/threelevelconditiondetect.h"

int main(int argc, char *argv[])
{


    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/LevelForSteel/Main.qml"));

    //保证只能存在一个运行实例
    QSharedMemory sharedMemory;
    sharedMemory.setKey("MyUniqueQtAppKey");
    if (!sharedMemory.create(1)) {
        QMessageBox::warning(nullptr, "警告", "程序已经在运行中!");
        return 1; // 程序退出
    }

    // Logger::iniLog("X_Level_Log",1024,false);

    // QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) {
    //         if (!obj && url == objUrl)
    //             QCoreApplication::exit(-1);
    //     }, Qt::QueuedConnection);

    // SteelServerResponse response;
    // response.startListen();



    // 测试功能
    // qDebug()<<"<<<";
    JsonFileHelper jsonHelper;
    NormalDetectBase *normalDetect = new  NormalDetectBase;
    VerticalLengthDetectBase *lengthDetect= new  VerticalLengthDetectBase;
    FrequencyDetectBase *frequenceDetect= new  FrequencyDetectBase;
    DirectLevelBase *directDetect = new  DirectLevelBase;
    BesideDetectBase *besideDetect= new  BesideDetectBase;
    QString jsonFlieName = "LevelForSteel.json";
    if(!jsonHelper.ReadJsonFile("LevelForSteel.json",*normalDetect,*lengthDetect,*frequenceDetect,*directDetect,*besideDetect)){
        jsonHelper.WriteJsonFile(jsonFlieName);
    };
    qDebug()<<"1";
    SteelInfo* steel = new SteelInfo;
    steel->setSteelName("Test001");
    steel->setSteelType("J1");
    steel->setTopLen(700000);

    ClientDefect *cliet1 =  new ClientDefect(1,1,16,227,100,26,"二类脱皮",20172,20399,-547,-531,1,0);
    ClientDefect *cliet2 =  new ClientDefect(1,2,25,688,100,26,"二类脱皮",20793,21481,-549,-524,1,0);
    ClientDefect *cliet3 =  new ClientDefect(1,3,16,301,100,26,"二类脱皮",24637,24938,-552,-536,1,0);
    ClientDefect *cliet4 =  new ClientDefect(1,4,19,1304,100,26,"二类脱皮",160951,162255,-548,-529,1,0);
    ClientDefect *cliet5 =  new ClientDefect(1,5,19,621,100,26,"二类脱皮",239228,239849,-547,-528,1,0);
    ClientDefect *cliet6 =  new ClientDefect(1,6,53,1356,100,26,"二类脱皮",244153,245509,-584,-531,1,0);
    ClientDefect *cliet7 =  new ClientDefect(1,7,23,816,100,26,"二类脱皮",660736,661552,-551,-528,1,0);
    ClientDefect *cliet8 =  new ClientDefect(1,8,57,794,100,26,"二类脱皮",664012,664532,-560,-527,1,0);
    ClientDefect *cliet9 =  new ClientDefect(1,9,48,779,100,26,"二类脱皮",665621,666415,-584,-527,1,0);
    ClientDefect *cliet10 = new ClientDefect(1,10,64,427,100,26,"二类脱皮",668803,669582,-583,-535,1,0);
    steel->ClientDefect1ist.append(cliet1);
    steel->ClientDefect1ist.append(cliet2);
    steel->ClientDefect1ist.append(cliet3);
    steel->ClientDefect1ist.append(cliet4);
    steel->ClientDefect1ist.append(cliet5);
    steel->ClientDefect1ist.append(cliet6);
    steel->ClientDefect1ist.append(cliet7);
    steel->ClientDefect1ist.append(cliet8);
    steel->ClientDefect1ist.append(cliet9);
    steel->ClientDefect1ist.append(cliet10);

    qDebug()<<"2";
    DefectOneHandle *oneHandle = new DefectOneHandle;
    DefectTwoHandle *twoHandle = new DefectTwoHandle;
    DefectThreeHandle *threeHandle = new DefectThreeHandle;
    threeHandle->SetNext(twoHandle)->SetNext(oneHandle);
    threeHandle->handle(*normalDetect,*lengthDetect,*frequenceDetect,*directDetect,*besideDetect,steel);


    engine.load(url);
    return app.exec();
}
